﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.SixAcquaint.Model;
using Yipusoft.SixAcquaint.BLL;
using System.Collections.Generic;

public partial class BasicBase_KeyUnit_FireForce : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            KeyUnitInfo keyUnitDetails = KeyUnit.GetKeyUnitInfoByID(Request["uid"]);
            lblKeyUnitName.Text = keyUnitDetails.UnitName;
            hlReturn.Text = "返回";
            hlReturn.NavigateUrl = "InsertKeyUnit.aspx?sid=" + keyUnitDetails.StandardID;
            hlReturn.ToolTip = "返回" + keyUnitDetails.UnitName;
            ListPageContral1.SetFirstPage();
            gvFFVehicle.PageIndex = 0;
            BindVehicle();
            UnitFireServiceInfo fireForceDetails = KeyUnit.GetFireForceInfoByID(Request["uid"]);
            if (fireForceDetails != null)
            {
                txtPrivateFireMen.Text = fireForceDetails.PrivateFireMen.ToString();
                txtFullTimeFireMen.Text = fireForceDetails.FullTimeFireMen.ToString();
                txtDutyType.Text = fireForceDetails.DutyType;
                txtCommandCenter.Text = fireForceDetails.CommandCenter;
                txtOthers.Text = fireForceDetails.Others;
                btnSave.Text = "修改";

            }

        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        gvFFVehicle.PageIndex = PageIndex;
        BindVehicle();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        UnitFireServiceInfo fireForce = new UnitFireServiceInfo();
        fireForce.KeyUnitID = Request["uid"];
        fireForce.PrivateFireMen = int.Parse(txtPrivateFireMen.Text);
        fireForce.FullTimeFireMen = int.Parse(txtFullTimeFireMen.Text);
        fireForce.DutyType = txtDutyType.Text;
        fireForce.CommandCenter = txtCommandCenter.Text;
        fireForce.Others = txtOthers.Text;
        if (btnSave.Text == "修改")
        {
            if (KeyUnit.UpdateFireForce(fireForce))
            {
                lblInfo.Text = "更新成功。";
            }
            else
            {
                lblInfo.Text = "更新失败！";
            }
        }
        else
        {

            if (KeyUnit.InsertFireForce(fireForce) == 1)
            {
                lblInfo.Text = "保存成功。";
            }
            else
            {
                lblInfo.Text = "保存失败！";
            }
        }

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(ddlPumper.SelectedItem.Text))
        {
            lblInfo2.Text = "请选择车辆！";
        }
        else
        {
            KeyUnitVehicleInfo vehicleDetails = new KeyUnitVehicleInfo();
            vehicleDetails.KeyUnitID = Request["uid"];
            vehicleDetails.VehicleType = ddlPumper.SelectedValue;
            vehicleDetails.Water = Convert.ToDecimal(txtWater.Text == "" ? "0" : txtWater.Text);
            vehicleDetails.Foam = Convert.ToDecimal(txtFoam.Text == "" ? "0" : txtFoam.Text);
            vehicleDetails.DryPowder = Convert.ToDecimal(txtDryPower.Text == "" ? "0" : txtDryPower.Text);
            if (KeyUnitVehicle.InsertKeyUnitVehicle(vehicleDetails))
            {
                lblInfo2.Text = "添加成功。";
                ListPageContral1.SetFirstPage();
                gvFFVehicle.PageIndex = 0;
                BindVehicle();
            }
            else
                lblInfo2.Text = "添加失败！";
        }
    }

    private void BindVehicle()
    {
        List<KeyUnitVehicleInfo> infos = KeyUnitVehicle.GetVehicleByKeyUnitID(Request["uid"]);
        ListPageContral1.ItemCount = infos.Count;
        gvFFVehicle.DataSource = infos;
        gvFFVehicle.DataBind();
    }
    protected void gvFFVehicle_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string vid = gvFFVehicle.DataKeys[e.RowIndex]["VehicleID"].ToString();
        if (KeyUnitVehicle.Delete(vid))
        {
            lblInfo2.Text = "删除成功。";
            BindVehicle();
        }
    }
    protected void gvFFVehicle_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            System.Web.UI.WebControls.Label lblID = e.Row.FindControl("lblID") as System.Web.UI.WebControls.Label;
            lblID.Text = Convert.ToString(gvFFVehicle.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
}